package com.yowasa.xiuxian.engine.message.cmd;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

@Component
@CMD(name = "/原神")
public class LockTimeHandler extends CmdMessageHandler {

    public static final Logger logger = LoggerFactory.getLogger(LockTimeHandler.class);
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;

    @Override
    public void execute(SendMessage message) {

        String param = getParam(message);
        if (StringUtils.isEmpty(param)) {
            throw new HandlerException("请在指令后追加 启动 或 关闭");
        }


        if ("关闭".equals(param)) {
            UserBO user = userHelper.checkOpUserAll(message);
            user.setLockEndTime(DateXUtil.getMaxTime());
            user.getUserConfig().setGetExpStartTime(new Date());
            user.setLocal(99);
            user.setLockReason("原神还没有启动QAQ\t暂时无法执行任何操作");
            userHelper.saveUserInfo(user);
            messageHelper.replyMys(message, "你下线了...");
        }
        if ("启动".equals(param)) {
            UserBO user = userHelper.checkOpUser(message);
            if(user.getLocal() != 99){
                throw new HandlerException("原神已经启动了");
            }
            Date date = new Date();
            long time = date.getTime();
            Date getExpStartTime = user.getUserConfig().getGetExpStartTime();
            long time1 = getExpStartTime.getTime();
            long l = Math.abs(time - time1);
            Date startAt = user.getStartAt();
            long l1 = startAt.getTime() + l;
            Date start = new Date(l1);

            user.setStartAt(start);
            MapEnum byName = MapEnum.getByName(user.getLocalName());
            user.setLocal(byName.getId());
            user.setLockEndTime(new Date());
            userHelper.saveUserInfo(user);
            messageHelper.replyMys(message, "原神~ 启动！！！");
        }
    }


    public String desc(){
        return new StringBuffer().append("为了学生党设计的指令，以免没有时间玩而老死\n下线的时候不会增加寿命，上线请使用 /原神 启动 指令")
                .toString();

    }

}
